package com.sunday.shoufen.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.MyRatingBar;
import com.sunday.shoufen.R;
import com.sunday.shoufen.base.BaseApp;
import com.sunday.shoufen.entity.BizShop;
import com.sunday.shoufen.http.ApiClient;
import com.sunday.shoufen.ui.ShopDetailActivity;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/10/19.
 */

public class BizShopListAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<BizShop> dataSet;
    private boolean isFromCollect=false;
    private View.OnClickListener onClickListener;

    public BizShopListAdapter(Context context, List<BizShop> datas,boolean isFromCollect) {
        this.mContext = context;
        this.dataSet = datas;
        this.isFromCollect=isFromCollect;
    }
    public BizShopListAdapter(Context context, List<BizShop> datas) {
        this.mContext = context;
        this.dataSet = datas;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.index_list_item, null, false);
        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ListHolder listHolder = (ListHolder) holder;
        BizShop item = dataSet.get(position);
        Glide.with(mContext)
                .load(item.getImage())
                .placeholder(R.mipmap.ic_default)
                .error(R.mipmap.ic_default)
                .into(listHolder.shopImg);
        listHolder.shopName.setText(item.getShopName());
        listHolder.shopDistance.setVisibility(isFromCollect?View.GONE:View.VISIBLE);
        listHolder.btnDel.setVisibility(isFromCollect?View.VISIBLE:View.GONE);
        listHolder.shopDistance.setText(item.getDistance());
        listHolder.ratingBar.setRating((int) item.getScore(), R.drawable.rating_bar, false, 30);
        listHolder.ratingScore.setText((int) item.getScore()+"分");
        listHolder.rlShop.setTag(item);
        listHolder.btnDel.setTag(position);
        listHolder.btnDel.setOnClickListener(onClickListener);

    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    class ListHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @Bind(R.id.shop_img)
        ImageView shopImg;
        @Bind(R.id.shop_name)
        TextView shopName;
        @Bind(R.id.rating_bar)
        MyRatingBar ratingBar;
        @Bind(R.id.rating_score)
        TextView ratingScore;
        @Bind(R.id.shop_desc)
        TextView shopDesc;
        @Bind(R.id.shop_distance)
        TextView shopDistance;
        @Bind(R.id.rl_shop)
        LinearLayout rlShop;
        @Bind(R.id.btn_del)
        ImageView btnDel;

        public ListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            rlShop.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.rl_shop:
                    BizShop item = (BizShop) v.getTag();
                    Intent intent = new Intent(mContext, ShopDetailActivity.class);
                    intent.putExtra("shopId", item.getShopId());
                    mContext.startActivity(intent);
                    break;
            }

        }
    }
}
